package com.badlogic.androidgames.framework.gl;


import java.util.List;

import org.w3c.dom.Element;

import com.turkline.CatchMePolygon.XmlManager;



public class FontBatcher
{
	 public final Texture texture;
	 public final float offsetX;
	 public final float offsetY;
	 public final List<Element> gypsList;
	 
	 public FontBatcher(Texture texture , float offsetX , float offsetY , List<Element> gypsList)
	 {
		 this.gypsList = gypsList;
		 this.texture = texture;
		 this.offsetX = offsetX;
		 this.offsetY = offsetY;
	 }
	 public void drawText(SpriteBatcher batcher, String text, float x, float y , float alpha ) 
	 {
		 int len = text.length();
	        
	        for(int i = 0; i < len; i++) 
	        {
	        	
	            int c = (int)(text.charAt(i));
	            int index = findIndex(c);
	            Element currentElement = gypsList.get(index);
	            TextureRegion glyph = new TextureRegion(texture,  Integer.parseInt(currentElement.getAttribute("x")) + offsetX,  Integer.parseInt(currentElement.getAttribute("y")) + offsetY, Integer.parseInt( currentElement.getAttribute("width")), Integer.parseInt(currentElement.getAttribute("height"))+3);
	            batcher.drawSprite(x, y, Integer.parseInt( currentElement.getAttribute("width")), Integer.parseInt( currentElement.getAttribute("height"))+2,alpha, glyph);
	            if(i< len-1)
	            {
	            	x += Integer.parseInt( currentElement.getAttribute("xadvance")) - Integer.parseInt( gypsList.get(findIndex(c)).getAttribute("xoffset"));
	            }
	            
	        }
	 }
	 
	private int findIndex(int c)
	{
		for(int i = 0 ; i < gypsList.size() ; i++)
		{
			if(Integer.parseInt(gypsList.get(i).getAttribute("id")) == c)
			{
				return i;
			}
		}
		return 0;
	}
}
